package com.frx.sort.countsort;

import java.util.Arrays;

/**
 * Created with IDEA
 *
 * @author: xrf
 * @Date: 2019/10/28
 * @Time: 17:01
 */
public class TestOne {

    public static void main(String[] args){
        int[] arr = {5,6,3,2,3,1,0};
        System.out.println(Arrays.toString(arr));
        countSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void countSort(int[] arr){
        int max = 0;
        for (int i=0;i<arr.length;i++){
            if (arr[i] > max){
                max = arr[i];
            }
        }
        int[] tmpArr = new int[max+1];
        for (int i=0;i<arr.length;i++){
            tmpArr[arr[i]]++;
        }
        System.out.println(Arrays.toString(tmpArr));
        for (int i=1;i<tmpArr.length;i++){
            tmpArr[i] = tmpArr[i] + tmpArr[i-1];
        }
        System.out.println(Arrays.toString(tmpArr));
        int[] resArr = new int[arr.length];
        for (int i=0;i<arr.length;i++){
            int index = arr[i];
            int count = tmpArr[index];
            resArr[count - 1] = index;
            tmpArr[index]--;
        }

        for (int i=0;i<arr.length;i++){
            arr[i] = resArr[i];
        }

    }


}
